using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

/// <summary>
/// AKS 2.0 - HiSoftware Inc.
/// SA DESIGN:
/// Robert B. Yonaitis 
/// </summary>

/// <variables>
/// oOutput          -  An object used in outputting the finalized code
/// sOutputBuilder   -  A string containing the code to be rendered to the page
/// oWriter          -  An object used to output the rendered code
/// sControlID       -  A string used to store the id of the control being affected
/// sSearchString    -  A string used to store the string being used to locate the correct control to alter
/// iStart           -  An integer which stores the starting index of the sSearchString when located
/// iStepThroughName -  An integer used as an index to step through sOutputBuilder
/// </variables>

namespace AKSAdapters
{
	public class AKS_Blog_EditItem_PublishedLabel_SmartAdapter : System.Web.UI.Adapters.ControlAdapter
	{
		public AKS_Blog_EditItem_PublishedLabel_SmartAdapter()
		{

		}

		protected override void Render(HtmlTextWriter oOutput)
		{
			//Initializes sOutputBuilder as a new StringBuilder object
			StringBuilder sOutputBuilder = new StringBuilder();
			HtmlTextWriter oWriter = new HtmlTextWriter(new StringWriter(sOutputBuilder));
			base.Render(oWriter);

			//If the page is EditComment.aspx then proceed with the alterations made by the adapter
			//Otherwise, the adapter will not change anything and proceeds to output.
			if (Page.Request.Url.ToString().Contains("/EditPost.aspx"))
			{
				//Initialize sControlID
				string sControlID = null;

				//Initialize sSearchString
				string sSearchString = null;

				//Assign sSearchString to the string used to identify the correct control
				sSearchString = "title=\"Published\"";

				//Initialize iStart as the starting index of sSearchString
				int iStart = sOutputBuilder.ToString().IndexOf(sSearchString);

				//If iStart is greater than 0, sSearchString has been found and the adapter will proceed with the alteration(s)
				//Otherwise, the adapter will not change anything and proceeds to output.
				if (iStart > 0)
				{
					//Search for the character '<', stepping back one space through sOutputBuilder while not found
					while (sOutputBuilder[iStart] != '<')
					{
						iStart -= 1;
					}

					//Initialize iStepThroughName to the local variable iStart used to step through sOutputBuilder 
					//after sSearchString has been found, proceeding until the end of the control's name attribute is found.
					int iStepThroughName = iStart;

					//Add 13 to iStepThroughName to pass by '<input name="'
					iStepThroughName += 13;

					//Step through sOutputBuilder until '"' is found
					while (sOutputBuilder[iStepThroughName] != '"')
					{
						//Assign the control's name attribute value to sControlID
						sControlID += sOutputBuilder[iStepThroughName];
						iStepThroughName++;
					}

					//Replace all instances of $ with _ to convert the name attribute value to the id attribute value
					sControlID = sControlID.Replace("$", "_");

					//Convert the "Published" text to a label for the "Published" text box 
					sOutputBuilder.Replace("<nobr>Published", "<nobr><label for=\"" + sControlID + "\">Published</label>");
				}
			}

			//Output sOutputBuilder to be rendered
			oOutput.Write(sOutputBuilder.ToString());
		}
	}
}



